package org.example.service.impl;

import org.example.dao.ClassroomDao;
import org.example.dto.ClassDto;
import org.example.dto.ClassroomDto;
import org.example.pojo.Classes;
import org.example.pojo.Classroom;
import org.example.service.ClassroomService;
import org.example.utils.Tool;
import org.example.vo.ClassVo;
import org.example.vo.ClassroomVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassroomServiceImpl implements ClassroomService {

    @Resource
    private ClassroomDao dao;
    private Tool tool=new Tool();

    private ClassroomDto PoTransToDto(Classroom classroom){
        ClassroomDto classroomDto=new ClassroomDto();
        classroomDto.setClassroomId(classroom.getRoomno());
        classroomDto.setClassroomName(classroom.getRoomname());
        classroomDto.setClassroomType(tool.checkCourseChracter(classroom.getRoomtype()));
        classroomDto.setBuilding(classroom.getBuildingname());
        classroomDto.setCapacity(classroom.getHold());
        return classroomDto;
    }

    private Classroom VoTransToPo(ClassroomVo classroomVo){
        Classroom classroom=new Classroom();
        classroom.setHold(classroomVo.getCapacity());
        classroom.setRoomno(classroomVo.getClassroomId());
        classroom.setRoomtype(tool.checkCourseNature(classroomVo.getClassroomType()));
        classroom.setRoomname(classroomVo.getClassroomName());
        classroom.setBuildingname(classroomVo.getBuilding());
//        classroom.setBuildingno();

        return classroom;
    }

    @Override
    public Integer addClassroom(ClassroomVo classroomVo) {
        if(dao.insert(VoTransToPo(classroomVo)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateClassroom(ClassroomVo classroomVo) {
        if(dao.update(VoTransToPo(classroomVo)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteClassroom(Integer classroomId) {
        if(dao.delete(classroomId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<ClassroomDto> queryAllClassroom() {
        List<Classroom> classroomList=dao.queryAll();
        List<ClassroomDto> classroomDtoList=new ArrayList<>();
        for(int i=0;i<classroomList.size();i++)
            classroomDtoList.add(i,PoTransToDto(classroomList.get(i)));
        return classroomDtoList;
    }

    @Override
    public Integer countClassroom() {
        return dao.queryAll().size();
    }

    @Override
    public ClassroomDto queryClassroomById(Integer classroomId) {
        Classroom classroom=new Classroom();
        classroom.setRoomno(classroomId);
        return PoTransToDto(dao.queryByCondition(classroom).get(0));
    }

    @Override
    public List<ClassroomDto> queryClassroomByBuilding(String buildingName) {
        Classroom classroom=new Classroom();
        classroom.setBuildingname(buildingName);
        List<Classroom> classroomList=dao.queryByCondition(classroom);
        List<ClassroomDto> classroomDtoList=new ArrayList<>();
        for(int i=0;i<classroomList.size();i++)
            classroomDtoList.add(i,PoTransToDto(classroomList.get(i)));
        return classroomDtoList;
    }

    @Override
    public Integer countClassroomByBuilding(String buildingName) {
        Classroom classroom=new Classroom();
        classroom.setBuildingname(buildingName);
        return dao.queryByCondition(classroom).size();
    }

    @Override
    public List<ClassroomDto> queryClassroomByBuildingAndClassroomType(String buildingName, String classroomType) {
        Classroom classroom=new Classroom();
        classroom.setBuildingname(buildingName);
        classroom.setRoomtype(tool.checkCourseNature(classroomType));
        List<Classroom> classroomList=dao.queryByCondition(classroom);
        List<ClassroomDto> classroomDtoList=new ArrayList<>();
        for(int i=0;i<classroomList.size();i++)
            classroomDtoList.add(i,PoTransToDto(classroomList.get(i)));
        return classroomDtoList;
    }

    @Override
    public Integer countClassroomByBuildingAndClassroomType(String buildingName, String classroomType) {
        Classroom classroom=new Classroom();
        classroom.setBuildingname(buildingName);
        classroom.setRoomtype(tool.checkCourseNature(classroomType));
        return dao.queryByCondition(classroom).size();
    }
}
